package com.aaa.stock.controller;


import com.aaa.entity.stock.Stock;
import com.aaa.stock.service.StockService;
import com.aaa.vo.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author aaa
 * @since 2020-11-28
 */
@RestController
@RequestMapping("/stock/stock")
public class StockController {
    @Autowired
    private StockService stockService;
    /*通过仓库id查询所有该仓库的库存*/
    @PostMapping("searchStock/{page}/{limit}")
    public R searchStock(@PathVariable Integer page, @PathVariable Integer limit, @RequestBody Stock stock){
        return stockService.searchStock(page,limit,stock);
    }
    /*通过商品编名称和仓库id查出对应的一个库存*/
    @GetMapping("selectStockByComBarOriLo")
    @ApiOperation("通过商品编名称和仓库id查出对应的一个库存")
    public Stock selectStockByComBarOriLo(@RequestParam String commoditybar, @RequestParam String locatorcode, @RequestParam Long warehouseid){
        return stockService.selectStockByComBarOriLo(commoditybar,locatorcode,warehouseid);
    }
    /*根据传入stock的实体id更新该stock*/
    @PostMapping("updateById")
    @ApiOperation("根据传入stock的实体id更新该stock")
    public Boolean updateById(@RequestBody Stock stock){
        return stockService.updateById(stock);
    }

    /*根据传入stock的实体id删除该stock*/
    @GetMapping("removeById")
    @ApiOperation("根据传入stock的实体id删除该stock")
    public Boolean removeById(@RequestParam Long id){
        return stockService.removeById(id);
    }
    /*传入实体增加一行stock*/
    @PostMapping("save")
    @ApiOperation("传入实体增加一行stock")
    public Boolean save(@RequestBody Stock stock){
        return stockService.save(stock);
    }
    /*传入一个实体和一个条件根据条件将实体更新到这个条件对应的一行数据*/
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ApiOperation("传入一个实体和一个条件根据条件将实体更新到这个条件对应的一行数据")
    public Boolean update(@RequestBody Stock stock, @RequestParam Map<String,Object> map){
        QueryWrapper<Stock> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("locatorcode",map.get("locatorcode"));
        queryWrapper.eq("commoditybar",map.get("commoditybar"));
        queryWrapper.eq("warehouseid",map.get("warehouseid"));
        return stockService.update(stock,queryWrapper);
    }

    @PostMapping("selectCommoditybarByWarehouseid")
    @ApiOperation(value = "根据仓库ID获取商品编码")
    public List<String> selectCommoditybarByWarehouseid(@ApiParam(value = "仓库ID")@RequestParam Long warehouseid){
        return stockService.selectCommoditybarByWarehouseid(warehouseid);
    }

    @PostMapping("selectSumUqtyByCommoditybar")
    @ApiOperation(value = "根据商品编码和仓库ID查询可用商品数量")
    public Integer selectSumUqtyByCommoditybar(@ApiParam(value = "商品编码") @RequestParam String commoditybar,@ApiParam(value = "仓库ID")@RequestParam Long warehouseid){
        return stockService.selectSumUqtyByCommoditybar(commoditybar,warehouseid);
    }

    @PostMapping("selectStockByCommoditybar")
    @ApiOperation(value = "根据商品编码和仓库ID查询库存")
    public List<Stock> selectStockByCommoditybar(@ApiParam(value = "商品编码") @RequestParam String commoditybar, @ApiParam(value = "仓库ID")@RequestParam Long warehouseid){
        return stockService.selectStockByCommoditybar(commoditybar,warehouseid);
    }
    /*通过货位编码和仓库id查出对应的所有库存*/
    @GetMapping("selectStockByLoCodeWareId")
    @ApiOperation("通过货位编码和仓库id查出对应的所有库存")
    public List<Stock> selectStockByLoCodeWareID(@RequestParam String locatorcode,
                                                 @RequestParam Long warehouseid){
        QueryWrapper<Stock> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("locatorcode",locatorcode);
        queryWrapper.eq("warehouseid",warehouseid);
        return stockService.list(queryWrapper);
    }

    /*审核入库方法*/
    @PostMapping("inboundStock")
    @ApiOperation("审核入库方法")
    public R inboundStock(@RequestBody Stock stock){
        QueryWrapper<Stock> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("locatorid",stock.getLocatorid());
        queryWrapper.eq("commodityid",stock.getCommodityid());
        Stock list = stockService.getOne(queryWrapper);
        if (list==null){
            stockService.saveOrUpdate(stock);
            return R.ok().code(200).message("商品入库存成功");
        }else{
            list.setCqty(list.getCqty()+stock.getCqty());
            list.setUqty(list.getUqty()+stock.getUqty());
            stockService.saveOrUpdate(list);
            return R.ok().code(201).message("商品库存增加成功");
        }
    }

    /*撤销审核入库方法*/
    @PostMapping("cinboundStock")
    @ApiOperation("撤销审核入库方法")
    public R cinboundStock(@RequestBody Stock stock){
        QueryWrapper<Stock> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("locatorid",stock.getLocatorid());
        queryWrapper.eq("commodityid",stock.getCommodityid());
        Stock list = stockService.getOne(queryWrapper);
        if (list==null){
            stockService.saveOrUpdate(stock);
            return R.ok().code(200).message("商品入库存成功");
        }else{
            int cty = list.getCqty()-stock.getCqty();
            int uty = list.getUqty()-stock.getUqty();
            if (cty<0 || uty<0){
                return R.error().code(500).message("商品已出库，不能撤销审核");
            }
            list.setCqty(list.getCqty()-stock.getCqty());
            list.setUqty(list.getUqty()-stock.getUqty());
            stockService.saveOrUpdate(list);
            return R.ok().code(200).message("商品库存增加成功");
        }
    }

    /*其他仓库库存*/
    @PostMapping("selectStoCom")
    @ApiOperation("其他仓库库存")
    public List<Stock> selectStoCom(){
        return stockService.selectStoCom();
    }

    @PostMapping("searchCommodity")
    @ApiOperation("查询不在入库区的商品库存")
    public R searchCommodity(){
        List<Stock> stocks = stockService.selectStockByHouse();
        return R.ok().code(200).data("stocks",stocks);
    }

    @PostMapping("selectStockByCommodity")
    @ApiOperation("根据货位编码和商品编码获取一个Stock")
    public Stock selectStockByCommodity(@RequestParam String locatorcode,@RequestParam String commoditybar){
        return stockService.selectStockByCommodity(locatorcode,commoditybar);
    }
}

